﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.TextUtils;
using CatUtilsDll.Utils.Currency.WebUtils;
using System;
using System.Collections.Generic;
using System.Net;

namespace CatUtils.Utils.SpecialUtils.ContentHelper.bangumi
{
    internal class BangGumiHelper
    {
        /// <summary>
        /// 搜索API
        /// </summary>
        public static string SearchSubjectApi = "http://bangumi.tv/subject_search/{0}?cat=all";

        /// <summary>
        /// 搜索人物API
        /// </summary>
        public static string SearchCharacterApi = "http://bangumi.tv/mono_search/{0}?cat=all";

        /// <summary>
        /// 主域名
        /// </summary>
        public static string BanggumiHost = "http://bangumi.tv";

        /// <summary>
        /// 自动搜索人物和动漫信息
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>

        public static string SearchAuto(string Name)
        {
            string Content = SearchAnimenName(Name);

            if (Content == "")
                Content = SearchCharacterName(Name);
            return Content;
        }

        /// <summary>
        /// 搜索人物名称并返回词条。
        /// </summary>
        /// <param name="CharacterName"></param>
        /// <returns></returns>
        public static string SearchCharacterName(String CharacterName)
        {
            return Search(SearchCharacterApi, CharacterName);
        }

        /// <summary>
        /// 获取动漫信息
        /// </summary>
        /// <param name="AnimenName"></param>
        /// <returns></returns>
        public static string SearchAnimenName(String AnimenName)
        {
            return Search(SearchSubjectApi, AnimenName);
        }

        /// <summary>
        /// 获取动漫信息
        /// </summary>
        /// <param name="AnimenName"></param>
        /// <returns></returns>
        public static string Search(string API, String AnimenName)
        {
            API = string.Format(API, Transcoding.UrlEncode(AnimenName));
            Console.WriteLine(API);
            string Shtml = WebHelper.HttpGet(API, GetHeader());
            string Link = GetArticle(Shtml);
            if (Link == "")
                return "";
            Console.WriteLine("扫描百科：" + BanggumiHost + Link);
            Shtml = WebHelper.HttpPcGet(BanggumiHost + Link);
            string InfoBox = GetAmimenInfo(Shtml);

            string Comment = GetComment(Shtml);

            return InfoBox + Comment;
        }

        /// <summary>
        /// 获取一个文章
        /// </summary>
        /// <param name="Shtml"></param>
        /// <returns></returns>
        private static string GetArticle(string Shtml)
        {
            List<string> ArticleList = HtmlHelper.GetLinkVlaueList(Shtml, " //div[@class='light_odd clearit']/a  | //ul[@id='browserItemList']/li/a");
            return ListHelper.GetFirst(ArticleList);
            //获取文章列表
        }

        /// <summary>
        /// 获取文章评论列表
        /// </summary>
        /// <param name="shtml"></param>
        /// <returns></returns>
        private static string GetComment(string Shtml)
        {
            string CommentLink = HtmlHelper.GetFirstLink(Shtml, "//div[@class='subject_section']/a");
            string TempContent = "";
            List<string> CommentList = GetCommentList(CommentLink);
            foreach (var item in CommentList)
            {
                if (item.Length < 15)
                    continue;
                TempContent += "\r\n" + item;
            }
            if (TempContent != "")

                return HtmlCodeCreate.LableDiv(TempContent, "user-comment");
            return "";
        }

        /// <summary>
        /// 搜索需要有Cookies参数
        /// </summary>
        /// <returns></returns>
        private static WebHeaderCollection GetHeader()
        {
            WebHeaderCollection webHeader = WebHelper.PcWebHeader();
            webHeader.Add("Cookie: __utmz=1.1553046783.10.8.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; chii_sid=Zmqtmn; __utmc=1; __utmb=1.33.10.1553049086; chii_searchDateLine=0; __utmt=1");
            return webHeader;
        }

        /// <summary>
        /// 获取介绍，来源地址
        /// </summary>
        /// <param name="Shtml"></param>
        /// <returns></returns>
        private static string GetAmimenInfo(string Shtml)
        {
            Shtml = Transcoding.HtmlDecode(Shtml);
            string InfoContent = "";
            string IntroduceContent = HtmlHelper.GetFirstText(Shtml, "//div[@id='subject_summary'] | //div[@class='detail']").Trim();
            foreach (var item in HtmlHelper.GetTextValueList(Shtml, "//ul[@id='infobox']/li"))
            {
                InfoContent += "\r\n" + item.Trim();
            }
            if (InfoContent != "")
                InfoContent = HtmlCodeCreate.LableDiv(InfoContent, "b-info");
            if (IntroduceContent != "")
                IntroduceContent = HtmlCodeCreate.LableDiv(IntroduceContent, "b-introduce");
            return IntroduceContent + InfoContent;
        }

        /// <summary>
        /// 获取评论列表
        /// </summary>
        /// <param name="CommentLink"></param>
        /// <returns></returns>
        private static List<string> GetCommentList(string CommentLink)
        {
            List<string> CommentList = new List<string>();
            if (CommentLink == "")
            {
                Console.WriteLine("没有评论。");
                return CommentList;
            }
            string CommentAPI = BanggumiHost + CommentLink;
            Console.WriteLine("评论链接：" + CommentAPI);
            string Shtml = WebHelper.HttpPcGet(CommentAPI);
            Console.WriteLine(HtmlHelper.GetFirstText(Shtml, "//div[@class='page_inner']/a[last()-1]").Trim());
            int MaxPage = TextHelper.StringToInt(HtmlHelper.GetFirstText(Shtml, "//div[@class='page_inner']/a[last()-1]").Trim());
            Console.WriteLine("最大页面：" + MaxPage);
            if (MaxPage == 0)
                return CommentList;
            int RadomPage = TextHelper.GetRandom(1, MaxPage);
            Shtml = WebHelper.HttpPcGet(CommentAPI + "?page=" + RadomPage);
            Console.WriteLine("获取随机页面评论：" + RadomPage);
            CommentList = HtmlHelper.GetTextValueList(Shtml, "//div[@class='item clearit']/div/div/p");
            ListHelper.Sort(CommentList, true);
            return CommentList;
        }
    }
}